package com.hankol.winecellar.adapter

import android.view.View
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.baselibrary.Constants
import com.baselibrary.extension.checkValue
import com.baselibrary.extension.load
import com.baselibrary.extension.setMallPriceTTF
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.*
import com.baselibrary.view.RoundCornerImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.winecellar.R
import com.hankol.winecellar.bean.GoodsOrderListBean
import com.hankol.winecellar.bean.OrderListSkuDtoBean
import com.baselibrary.utils.AmountUtil
import com.baselibrary.utils.OptionUtils

/**新零售-二期-销售订单 列表
 */
class GoodsOrderListAdapter : BaseQuickAdapter<GoodsOrderListBean, BaseViewHolder>(R.layout.item_goods_order_list) {

    override fun convert(helper: BaseViewHolder, item: GoodsOrderListBean) {
        helper.setText(R.id.tv_order_store_name, "订单编号 ${item.orderCode.checkValue()}")
        helper.setText(R.id.tv_order_state, OptionUtils.getOrderStatusString(item.status))
        helper.addOnClickListener(R.id.rl_order_multiple)
        helper.getView<TextView>(R.id.tv_unit).setMallPriceTTF(mContext)
        val llOrderMultiple = helper.getView<RelativeLayout>(R.id.rl_order_multiple)
        val llOrderSingle = helper.getView<LinearLayout>(R.id.ll_order_single)
        if (item.orderListSkuDtoList.size > 1) {
            llOrderMultiple.visibility = View.VISIBLE
            llOrderSingle.visibility = View.GONE
            val rvOrderCover = helper.getView<RecyclerView>(R.id.rv_order_cover)
            val orderCoverListAdapter = OrderCoverListAdapter()
            rvOrderCover.adapter = orderCoverListAdapter
            orderCoverListAdapter.setNewData(item.orderListSkuDtoList)
            orderCoverListAdapter.setOnItemClickListener { adapter, view, position ->
                //进入订单详情
                val hashMap = HashMap<String, Any>()
                hashMap[Constants.KEY_ORDER_ID] = item.poId
                ARouterUtils.jump(ActivityRouter.WINE_CELLAR.ORDER_DETAIL, hashMap)
            }
            helper.setText(R.id.tv_order_sum_price, "${AmountUtil.changeF2YKeepTwo("${item.price}")}")
            helper.setText(R.id.tv_order_sum_quantity, "共${item.num}件")
        } else if (item.orderListSkuDtoList.size == 1) {
            llOrderMultiple.visibility = View.GONE
            llOrderSingle.visibility = View.VISIBLE
            val product = item.orderListSkuDtoList[0]
            GlideUtils.loadImg_228(mContext, product.skuCover.checkValue(), helper.getView(R.id.round_order_cover))
            helper.setText(R.id.tv_order_title, product.spuName.checkValue())
            helper.setText(R.id.tv_order_norms, "${OptionUtils.getNormsStr(product.specList)}")
            helper.setText(R.id.tv_order_price, "${AmountUtil.changeF2YKeepTwo("${item.price}")}")
            helper.setText(R.id.tv_order_quantity, "数量：${product.num}")
            helper.getView<TextView>(R.id.tv_order_price).setMallPriceTTF(mContext)
            helper.setGone(R.id.iv_gift_label,product.giftType == 1)
        }
        helper.addOnClickListener(R.id.tv_send_goods)
        helper.addOnClickListener(R.id.rl_order_list_item)
        helper.addOnClickListener(R.id.ll_price_num)
        helper.setGone(R.id.rl_bot_button, item.status == 1)
        helper.setGone(R.id.tv_send_goods, item.status == 1)
        helper.setText(R.id.tv_send_goods, "发货")
        //是否存在售后
        helper.setBackgroundRes(R.id.tv_send_goods, if (item.afterSaleStatus == -1) R.drawable.shape_f54d53_stroke_13dp else R.drawable.shape_f54d53_stroke_13dp_a50)
        helper.setTextColor(R.id.tv_send_goods, ContextCompat.getColor(mContext, if (item.afterSaleStatus == -1) R.color.color_D53 else R.color.color_50_F54D53))
        helper.setEnabled(R.id.tv_send_goods, item.afterSaleStatus == -1)
    }

    class OrderCoverListAdapter : BaseQuickAdapter<OrderListSkuDtoBean, BaseViewHolder>(R.layout.item_order_cover_list) {
        override fun convert(helper: BaseViewHolder, item: OrderListSkuDtoBean) {
            helper.setGone(R.id.iv_gift_label, item.giftType == 1)
            helper.getView<RoundCornerImageView>(R.id.cover).load(mContext, item.skuCover.checkValue())
            if (itemCount - 1 == helper.adapterPosition) {
                helper.setGone(R.id.iv_cover_station, true)
            } else {
                helper.setGone(R.id.iv_cover_station, false)
            }
        }
    }
}

